package com.jushu.storbox.service;

import java.util.List;

import com.jushu.storbox.database.DAOFactory;
import com.jushu.storbox.entities.FileInfo;
import com.jushu.storbox.entities.FolderInfo;
import com.jushu.storbox.filesystem.FileManagerFactory;
import com.jushu.storbox.filesystem.iFileManager;
import com.huawei.sharedrive.sdk.android.modelV2.response.FileInfoResponseV2;


public class LocalShareFileService extends AbstractLocalService implements ILocalShareFileService {

	@Override
	public long insertFile(FileInfoResponseV2 fileInfo) {
		return DAOFactory.instance(this.getContext()).getShareFileDao().insertFile(fileInfo);
	}

	@Override
	public void updateFile(FileInfoResponseV2 fileInfoResponse) {
		DAOFactory.instance(this.getContext()).getShareFileDao().updateFile(fileInfoResponse,false);
	}

	@Override
	public void updateFile(FileInfo fileInfo) {
		DAOFactory.instance(this.getContext()).getShareFileDao().updateFile(fileInfo);
	}

	@Override
	public void inserOrUpdateFile(FileInfoResponseV2 fileInfo) {
		// TODO
	}

	@Override
	public List<FileInfo> getFileList(String folderId,String ownerBy) {
		return DAOFactory.instance(this.getContext()).getShareFileDao().getFileList(folderId, ownerBy);
	}

	@Override
	public FileInfo getFileById(String fileId ,String ownerBy) {
		return DAOFactory.instance(this.getContext()).getShareFileDao().getFileById(fileId, ownerBy);
	}

	@Override
	public void deleteFile(String fileID,String ownerBy) {
		FileInfo fileInfo = DAOFactory.instance(this.getContext()).getShareFileDao().getFileById(fileID, ownerBy);
		iFileManager mdmTools = FileManagerFactory.getFileManager(this.getContext());
		mdmTools.deleteFile(fileInfo.getLoctPath());
		DAOFactory.instance(this.getContext()).getShareFileDao().deleteFile(fileID, ownerBy);
	}

	@Override
	public void renameFile(String name, String fileId, String filePath) {
	}

	@Override
	public void moveFile(String parentID, String folderID) {
	}

	/**
	 * @param folderId
	 * @param folderName
	 */
	public void syncFile(String fileId, String fileName,String ownerBy) {
		DAOFactory.instance(this.getContext()).getShareFileDao().updateSyncStatus(1, fileId, ownerBy);
	}

	/**
	 * @param folderId
	 * @param folderName
	 */
	public void cancelSyncFile(String fileId, String fileName,String ownerBy) {
		FileInfo fileInfo = DAOFactory.instance(this.getContext()).getShareFileDao().getFileById(fileId, ownerBy);
		if (fileInfo == null) {
			return;
		}
		String parentFolderId = fileInfo.getParent();
		FolderInfo parent = DAOFactory.instance(this.getContext()).getShareFolderDao().getFolderById(parentFolderId, ownerBy);
		if (parent != null && parent.isSync()) {
			DAOFactory.instance(this.getContext()).getShareFolderDao().updateSyncStatuc(0, parentFolderId, ownerBy);
		}
		DAOFactory.instance(this.getContext()).getFileDao().updateSyncStatus(ownerBy, fileId, 0);
	}

	@Override
	public void updateLocalPath(String fileId, String localPath,String ownerBy) {
		DAOFactory.instance(this.getContext()).getShareFileDao().updateLocalPath(fileId, ownerBy, localPath);
	}

	@Override
	public void updateTransStatus(String ownerId, int status, String fileId) {
		DAOFactory.instance(this.getContext()).getFileDao().updateTransStatus(ownerId, fileId, status);
	}

	@Override
	public void updateSyncStatus(String ownerId, int syncStatus, String fileId) {
		DAOFactory.instance(this.getContext()).getFileDao().updateSyncStatus(ownerId, fileId, syncStatus);
	}

	@Override
	public void syncFile(String fileId, String fileName) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public FileInfo selectShareFile(String fileId, String ownerId) {
		// TODO Auto-generated method stub
		return DAOFactory.instance(this.getContext()).getShareFileDao().getFileById(fileId, ownerId);
	}


	
}
